//
//  HFDateSelectViewController.m
//  HFCalendar
//
//  Created by Luffy on 2021/8/24.
//

#import "HFDateSelectViewController.h"
#import "HFCalendarView.h"
#import "HFCalendarHeader.h"
#import "HFCalendarHelper.h"

@interface HFDateSelectViewController ()

@property (nonatomic, strong) UIView *baseView;
@property (nonatomic, strong) HFCalendarView *calendarView;

@property (nonatomic, strong) HFCalendarHelper *helper;
@property (nonatomic, assign) CGFloat baseHeight;

@end

@implementation HFDateSelectViewController

#pragma mark - Life Cyle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.helper = [HFCalendarHelper defaultManager];
    self.baseHeight = (SCREEN_HEIGHT - 250);
    
    [self setupUI];
}

#pragma mark - Public Methods
- (void)showCalendarWithVC:(UIViewController *)VC {
    self.modalPresentationStyle = UIModalPresentationCustom;
    [VC presentViewController:self animated:NO completion:^{
        [UIView animateWithDuration:.3 animations:^{
            [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
            self.baseView.frame = CGRectMake(0, SCREEN_HEIGHT - self.baseHeight, SCREEN_WIDTH, self.baseHeight);
        } completion:nil];
    }];
}

- (void)dissmissCalendar {
    [UIView animateWithDuration:.3 animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        self.baseView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.baseHeight);
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

#pragma mark - Target Methods
- (void)sureBtnDidClick:(NSString *)dateStr {
    [self dissmissCalendar];
    
    if (self.date_callBack) {
        self.date_callBack(dateStr);
    }
}

#pragma mark - Intial Methods
- (void)setupUI {
    self.view.backgroundColor = HFHexColorA(0x000000, 0.5);
    
    self.baseView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.baseHeight)];
    self.baseView.backgroundColor = UIColor.whiteColor;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.baseView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.baseView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.baseView.layer.mask = maskLayer;
    [self.view addSubview:self.baseView];
    
    UIView *dissView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - self.baseHeight)];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dissmissCalendar)];
    [dissView addGestureRecognizer:singleTap];
    [self.view addSubview:dissView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, SCREEN_WIDTH, 50)];
    titleLabel.text = @"日期选择";
    titleLabel.textColor = CustomTitleColor;
    titleLabel.font = [UIFont boldSystemFontOfSize:17.f];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.baseView addSubview:titleLabel];
    
    self.calendarView = [[HFCalendarView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(titleLabel.frame), SCREEN_WIDTH, self.baseView.frame.size.height - 55) startDateString:self.helper.customDateStartDate endDateString:self.helper.customDateEndDate];
    self.calendarView.maxChooseNumber = self.maxChooseNumber;
    self.calendarView.dateArray = self.dateArray;

    __weak typeof(self)weakSelf = self;
    
    self.calendarView.sure_callBack = ^(NSString * _Nonnull dateStr) {
        [weakSelf sureBtnDidClick:dateStr];
    };
    
    [self.baseView addSubview:self.calendarView];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
